package com.space.util;

import com.space.entity.City;
import org.jetbrains.annotations.NotNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @author : 港某
 * @date : 2020/2/13 18:41
 */
public class MyUtil {
    public static boolean checkNumber(String num) {
        return Pattern.matches("(\\d+)", num);
    }

    public static String getToday() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date());
    }

    public static long stringDateToLong(String sdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(sdate);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String longDateToString(long l) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(l);
        return sdf.format(date);
    }

    public static String getEndTime(City startCity, City endCity, String flightStartTime) {
        double hour = Math.sqrt(Math.pow(startCity.getCityPointX() - endCity.getCityPointX(), 2)
                + Math.pow(startCity.getCityPointY() - endCity.getCityPointY(), 2)) / 800;
        long l1 = MyUtil.stringDateToLong(flightStartTime);
        return MyUtil.longDateToString((long) (l1 + 3600000 * hour));
    }

    public static boolean checkTime(String time) {
        int check = 0;
        int correct = 3;
        int maxYear = 9999;
        int maxMonth = 12;
        int maxDay = 31;
        String[] timeArr = time.split("[-]");
        if (timeArr.length == correct) {
            for (String timeSplit : timeArr) {
                if (MyUtil.checkNumber(timeSplit)) {
                    check++;
                }
            }
            if (correct == check) {
                if (Integer.parseInt(timeArr[0]) < maxYear) {
                    if (Integer.parseInt(timeArr[1]) < maxMonth) {
                        return Integer.parseInt(timeArr[2]) < maxDay;
                    }
                }
            }
        }
        return false;
    }

    public static void test(@NotNull String str) {
        System.out.println(str.length());
    }

    public static void main(String[] args) {
        MyUtil.test("abc");
    }
}
